package com.baomidou.ant.service.impl;

import com.baomidou.ant.entity.*;
import com.baomidou.ant.mapper.BagContentMapper;
import com.baomidou.ant.service.*;
import com.baomidou.ant.utils.MyStringUtils;
import com.baomidou.ant.vo.Request.RequestUseGoodsVo;
import com.baomidou.ant.vo.Response.ResponseMyBagListVo;
import com.baomidou.ant.vo.Response.ResponseMyPropsVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * 每一次使用物品，都涉及到:
 * 1，使用的是什么？道具还是饰品还是礼包？
 * 2，使用的是什么道具？什么饰品？什么礼包？
 * 3，使用一个物品后，关联的userBag(背包信息)，bagContent(背包内容物),信息要更改，尤其是使用礼包，还得更改道具数量
 *
 *
 * @author jobob
 * @since 2021-03-24
 */
@Service
public class BagContentServiceImpl extends ServiceImpl<BagContentMapper, BagContent> implements IBagContentService {
    @Autowired
    private IPointsStoreService pointsStoreService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IUserBagService userBagService;
    @Autowired
    private IEndlessInfoService endlessInfoService;
    @Autowired
    private IMyMusicService myMusicService;
    @Autowired
    private IMusicService musicService;

    @Override
    public List<ResponseMyBagListVo> listMyBag(Integer userId){
        //先通过用户id查找该用户所有的所有物
        QueryWrapper<BagContent> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        List<BagContent> bagContentList=list(queryWrapper);//所有的兑换清单
        //保存要返回的数据
        List<ResponseMyBagListVo> voList=new ArrayList<>();
        for(BagContent bagContent:bagContentList){
            //判断该道具是否有数量
            if(bagContent.getAmount()>0){
                PointsStore goods=pointsStoreService.getById(bagContent.getGoodsId());
                //先拿出商品名和id
                ResponseMyBagListVo vo=new ResponseMyBagListVo();
                vo.setGoodsId(bagContent.getGoodsId());
                //再拿出图片地址和商品类型和介绍
                vo.setGoodsImage(goods.getGoodsImage());
                vo.setAmount(bagContent.getAmount());
                //存入
                voList.add(vo);
            }
        }
        return voList;
    }

    @Override
    public String useGoods(RequestUseGoodsVo requestUseGoodsVo) {
        User user=userService.getById(requestUseGoodsVo.getUserId());
        //得到所用物品
        PointsStore goods=pointsStoreService.getById(requestUseGoodsVo.getGoodsId());
        //先找背包
        QueryWrapper<UserBag> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",requestUseGoodsVo.getUserId());
        UserBag userBag=userBagService.getOne(queryWrapper);
        //找背包内容物
        QueryWrapper<BagContent> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("user_id",requestUseGoodsVo.getUserId());
        queryWrapper1.eq("goods_id",requestUseGoodsVo.getGoodsId());
        BagContent bagContent=getOne(queryWrapper1);

        //判断使用的什么类型的物品
        if(Objects.equals(goods.getGoodsType(), "饰品")){
            user.setUserImage(goods.getGoodsImage());
            userService.saveOrUpdate(user);//保存
        }else if(Objects.equals(goods.getGoodsType(), "道具")){

            //判断使用的道具是啥
            if (Objects.equals(goods.getGoodsName(), "背包扩充卡")) {
                //背包信息变更
                userBag.setTotalAmount(userBag.getTotalAmount()+6);
                userBag.setUsedAmount(userBag.getUsedAmount()-1);
                if(userBag.getTotalAmount()==24){
                    userBag.setBagLevel("中级学者仓库");
                }else if(userBag.getTotalAmount()==36){
                    userBag.setBagLevel("高级学者仓库");
                }else if(userBag.getTotalAmount()==48){
                    userBag.setBagLevel("顶级学者仓库");
                }else if(userBag.getTotalAmount()>=60){
                    userBag.setBagLevel("至尊学者仓库");
                }
                userBagService.saveOrUpdate(userBag);
                //使用过了，因为这是唯一性道具
                /* removeById(bagContent.getId());*/
                bagContent.setAmount(bagContent.getAmount()-1);
                saveOrUpdate(bagContent);
                return "addBag";
            }else if(Objects.equals(goods.getGoodsName(),"魔法沙漏")||Objects.equals(goods.getGoodsName(),"pass秘籍")||Objects.equals(goods.getGoodsName(),"强心剂")||Objects.equals(goods.getGoodsName(),"万能血袋")){
                return "propsOfLevel";
            }else if(Objects.equals(goods.getGoodsName(),"改名卡")){
                return "propOfName";
            }else if(Objects.equals(goods.getGoodsName(),"复活十字架")){
                QueryWrapper<EndlessInfo> endlessInfoQueryWrapper=new QueryWrapper<>();
                endlessInfoQueryWrapper.eq("user_id",user.getId());
                EndlessInfo endlessInfo=endlessInfoService.getOne(endlessInfoQueryWrapper);
                if(endlessInfo.getNowLevel()!=1||endlessInfo.getErrorNum()==3){
                    return "errorRemake";
                }
                endlessInfo.setNowDone(endlessInfo.getLastDone());
                endlessInfo.setNowLevel(endlessInfo.getLastLevel());
                endlessInfo.setErrorNum(0);
                endlessInfoService.saveOrUpdate(endlessInfo);
                //背包信息变更
                userBag.setUsedAmount(userBag.getUsedAmount()-1);
                userBagService.saveOrUpdate(userBag);
                //使用过了，因为这是唯一性道具
                /*removeById(bagContent.getId());*/
                bagContent.setAmount(bagContent.getAmount()-1);
                saveOrUpdate(bagContent);
                return "remake";
            }else if(Objects.equals(goods.getGoodsName(),"唱片机")){
                //背包信息变更
                userBag.setUsedAmount(userBag.getUsedAmount()-1);
                userBagService.saveOrUpdate(userBag);
                //添加默认音乐
                Music music=musicService.getById(1);
                MyMusic myMusic=new MyMusic();
                BeanUtils.copyProperties(music,myMusic);
                myMusic.setIsUse(1);
                myMusic.setUserId(user.getId());
                myMusicService.save(myMusic);

                bagContent.setAmount(bagContent.getAmount()-1);
                saveOrUpdate(bagContent);

                return "musicPlayer";
            }

            /* 其他道具后续开写在这 */

        }else if(Objects.equals(goods.getGoodsType(), "礼包")){
            //判断是啥礼包
            if(Objects.equals(goods.getGoodsName(),"无尽初阶挑战礼包")){
                //调用道具增加方法
                addProps(requestUseGoodsVo.getUserId(),1);
                //背包信息变更
                userBag.setUsedAmount(userBag.getUsedAmount()-1);
                userBagService.saveOrUpdate(userBag);
                //使用过了,改变背包内容物
                bagContent.setAmount(bagContent.getAmount()-1);
                saveOrUpdate(bagContent);
            }else if(Objects.equals(goods.getGoodsName(),"无尽高阶挑战礼包")){
                //调用道具增加方法
                addProps(requestUseGoodsVo.getUserId(),3);
                //背包信息变更
                userBag.setUsedAmount(userBag.getUsedAmount()-1);
                userBagService.saveOrUpdate(userBag);
                //使用过了,改变背包内容物
                bagContent.setAmount(bagContent.getAmount()-1);
                saveOrUpdate(bagContent);
                System.out.println(userBag.getUsedAmount());
            }else if(Objects.equals(goods.getGoodsName(),"激进音乐集")){
                return "musicCollection";
            }

            /* 其他礼包写在这 */

            //礼包统一返回字段
            return "giftPackage";
        }else if(Objects.equals(goods.getGoodsType(), "唱片")){
            //背包信息变更
            userBag.setUsedAmount(userBag.getUsedAmount()-1);
            userBagService.saveOrUpdate(userBag);
            //添加音乐
            QueryWrapper<Music> musicQueryWrapper=new QueryWrapper<>();
            musicQueryWrapper.eq("music_name",MyStringUtils.removeStr(goods.getGoodsName(),"唱片"));
            Music music=musicService.getOne(musicQueryWrapper);
            MyMusic myMusic=new MyMusic();
            BeanUtils.copyProperties(music,myMusic);
            myMusic.setUserId(user.getId());
            myMusicService.save(myMusic);
            //背包内容物更改
            bagContent.setAmount(bagContent.getAmount()-1);
            saveOrUpdate(bagContent);

            return "music";
        }

        return "饰品";
    }

    @Override
    public List<ResponseMyPropsVo> myProps(Integer userId) {
        List<ResponseMyPropsVo> voList=new ArrayList<>();
        //关卡道具的id:9
        QueryWrapper<BagContent> queryWrapper9=new QueryWrapper<>();
        queryWrapper9.eq("user_id",userId);
        queryWrapper9.like("goods_id",9);
        BagContent addTime=getOne(queryWrapper9);
        if(addTime==null){
            ResponseMyPropsVo vo=new ResponseMyPropsVo();
            PointsStore goods=pointsStoreService.getById(9);
            BeanUtils.copyProperties(goods,vo);
            vo.setGoodsId(9);
            vo.setAmount(0);
            voList.add(vo);
        }else{
            ResponseMyPropsVo vo=new ResponseMyPropsVo();
            BeanUtils.copyProperties(addTime,vo);
            PointsStore goods=pointsStoreService.getById(addTime.getGoodsId());
            vo.setGoodsImage(goods.getGoodsImage());
            voList.add(vo);
        }

        //关卡道具的id:10
        QueryWrapper<BagContent> queryWrapper10=new QueryWrapper<>();
        queryWrapper10.eq("user_id",userId);
        queryWrapper10.like("goods_id",10);
        BagContent pass=getOne(queryWrapper10);
        if(pass==null){
            ResponseMyPropsVo vo=new ResponseMyPropsVo();
            PointsStore goods=pointsStoreService.getById(10);
            BeanUtils.copyProperties(goods,vo);
            vo.setGoodsId(10);
            vo.setAmount(0);
            voList.add(vo);
        }else{
            ResponseMyPropsVo vo=new ResponseMyPropsVo();
            BeanUtils.copyProperties(pass,vo);
            PointsStore goods=pointsStoreService.getById(pass.getGoodsId());
            vo.setGoodsImage(goods.getGoodsImage());
            voList.add(vo);
        }

        //关卡道具的id:11
        QueryWrapper<BagContent> queryWrapper11=new QueryWrapper<>();
        queryWrapper11.eq("user_id",userId);
        queryWrapper11.like("goods_id",11);
        BagContent addBloodMini=getOne(queryWrapper11);
        if(addBloodMini==null){
            ResponseMyPropsVo vo=new ResponseMyPropsVo();
            PointsStore goods=pointsStoreService.getById(11);
            BeanUtils.copyProperties(goods,vo);
            vo.setGoodsId(11);
            vo.setAmount(0);
            voList.add(vo);
        }else{
            ResponseMyPropsVo vo=new ResponseMyPropsVo();
            BeanUtils.copyProperties(addBloodMini,vo);
            PointsStore goods=pointsStoreService.getById(addBloodMini.getGoodsId());
            vo.setGoodsImage(goods.getGoodsImage());
            voList.add(vo);
        }


        //关卡道具的id:12
        QueryWrapper<BagContent> queryWrapper12=new QueryWrapper<>();
        queryWrapper12.eq("user_id",userId);
        queryWrapper12.like("goods_id",12);
        BagContent addBlood=getOne(queryWrapper12);
        if(addBlood==null){
            ResponseMyPropsVo vo=new ResponseMyPropsVo();
            PointsStore goods=pointsStoreService.getById(12);
            BeanUtils.copyProperties(goods,vo);
            vo.setGoodsId(12);
            vo.setAmount(0);
            voList.add(vo);
        }else{
            ResponseMyPropsVo vo=new ResponseMyPropsVo();
            BeanUtils.copyProperties(addBlood,vo);
            PointsStore goods=pointsStoreService.getById(addBlood.getGoodsId());
            vo.setGoodsImage(goods.getGoodsImage());
            voList.add(vo);
        }

        //关卡道具的id:13
        QueryWrapper<BagContent> queryWrapper13=new QueryWrapper<>();
        queryWrapper13.eq("user_id",userId);
        queryWrapper13.like("goods_id",13);
        BagContent remake=getOne(queryWrapper13);
        if(remake==null){
            ResponseMyPropsVo vo=new ResponseMyPropsVo();
            PointsStore goods=pointsStoreService.getById(13);
            BeanUtils.copyProperties(goods,vo);
            vo.setGoodsId(13);
            vo.setAmount(0);
            voList.add(vo);
        }else{
            ResponseMyPropsVo vo=new ResponseMyPropsVo();
            BeanUtils.copyProperties(remake,vo);
            PointsStore goods=pointsStoreService.getById(remake.getGoodsId());
            vo.setGoodsImage(goods.getGoodsImage());
            voList.add(vo);
        }

        return voList;
    }

    @Override
    public String useProps(RequestUseGoodsVo vo) {
        PointsStore prop=pointsStoreService.getById(vo.getGoodsId());
        //无尽信息
        QueryWrapper<EndlessInfo> endlessInfoQueryWrapper=new QueryWrapper<>();
        endlessInfoQueryWrapper.eq("user_id",vo.getUserId());
        EndlessInfo endlessInfo=endlessInfoService.getOne(endlessInfoQueryWrapper);
        //背包内容物
        QueryWrapper<BagContent> bagContentQueryWrapper=new QueryWrapper<>();
        bagContentQueryWrapper.eq("user_id",vo.getUserId());
        bagContentQueryWrapper.eq("goods_id",vo.getGoodsId());
        BagContent bagContent=getOne(bagContentQueryWrapper);
        //背包
        QueryWrapper<UserBag> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",vo.getUserId());
        UserBag userBag=userBagService.getOne(queryWrapper);
        //定义返回语句
        String s=null;
        if(Objects.equals(prop.getGoodsName(), "魔法沙漏")){
            s="addTime";
        }
        if(Objects.equals(prop.getGoodsName(), "复活十字架")){
            s="remake";
        }
        if(Objects.equals(prop.getGoodsName(), "pass秘籍")){
            endlessInfo.setNowLevel(endlessInfo.getNowLevel()+1);
            endlessInfo.setLastLevel(endlessInfo.getLastLevel()+1);
            s="pass";
        }
        if(Objects.equals(prop.getGoodsName(), "强心剂")){
            if(endlessInfo.getErrorNum()==3){
                s="badRequest";
            }else {
                endlessInfo.setErrorNum(endlessInfo.getErrorNum()+1);
                s="addBloodMini";
            }
        }
        if(Objects.equals(prop.getGoodsName(), "万能血袋")){
            if(endlessInfo.getErrorNum()==3){
                s="badRequest";
            }else {
                endlessInfo.setErrorNum(3);
                s="addBlood";
            }
        }
        //背包更新
        bagContent.setAmount(bagContent.getAmount()-1);
        saveOrUpdate(bagContent);
        if(bagContent.getAmount()==0){
            userBag.setUsedAmount(userBag.getUsedAmount()-1);
            /*这里不删除数量为零的道具*/
        }
        //保存
        endlessInfoService.saveOrUpdate(endlessInfo);
        userBagService.saveOrUpdate(userBag);

        return s;
    }


    @Override
    public void addProps(Integer userId,Integer amount) {
        //背包内容物
        QueryWrapper<BagContent> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.in("goods_id",9,10,11,12,13);
        QueryWrapper<UserBag> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("user_id",userId);
        UserBag userBag=userBagService.getOne(queryWrapper1);
        //已经持有的道具集合
        List<BagContent> bagContentList=list(queryWrapper);
        //定义一个持有道具id的集合
        List<Integer> bagContentId=new ArrayList<>();
        for(BagContent bagContent:bagContentList){
            //纪录id
            bagContentId.add(bagContent.getGoodsId());
        }
        //没有的道具也要给出，数量为0
        if(!bagContentId.contains(9)){
            PointsStore goods=pointsStoreService.getById(9);
            //没有魔法沙漏
            BagContent bagContent=new BagContent();
            BeanUtils.copyProperties(goods,bagContent);
            bagContent.setUserId(userId);
            bagContent.setGoodsId(9);
            bagContent.setAmount(0);
            save(bagContent);
            bagContentList.add(bagContent);
            //背包更新
            userBag.setUsedAmount(userBag.getUsedAmount()+1);
        }
        if(!bagContentId.contains(10)){
            PointsStore goods=pointsStoreService.getById(10);
            BagContent bagContent=new BagContent();
            BeanUtils.copyProperties(goods,bagContent);
            bagContent.setUserId(userId);
            bagContent.setGoodsId(10);
            bagContent.setAmount(0);
            save(bagContent);
            bagContentList.add(bagContent);
            //背包更新
            userBag.setUsedAmount(userBag.getUsedAmount()+1);
        }
        if(!bagContentId.contains(11)){
            PointsStore goods=pointsStoreService.getById(11);
            BagContent bagContent=new BagContent();
            BeanUtils.copyProperties(goods,bagContent);
            bagContent.setUserId(userId);
            bagContent.setGoodsId(11);
            bagContent.setAmount(0);
            save(bagContent);
            bagContentList.add(bagContent);
            //背包更新
            userBag.setUsedAmount(userBag.getUsedAmount()+1);
        }
        if(!bagContentId.contains(12)){
            PointsStore goods=pointsStoreService.getById(12);
            BagContent bagContent=new BagContent();
            BeanUtils.copyProperties(goods,bagContent);
            bagContent.setUserId(userId);
            bagContent.setGoodsId(12);
            bagContent.setAmount(0);
            save(bagContent);
            bagContentList.add(bagContent);
            //背包更新
            userBag.setUsedAmount(userBag.getUsedAmount()+1);
        }
        if(!bagContentId.contains(13)){
            PointsStore goods=pointsStoreService.getById(13);
            BagContent bagContent=new BagContent();
            BeanUtils.copyProperties(goods,bagContent);
            bagContent.setUserId(userId);
            bagContent.setGoodsId(13);
            bagContent.setAmount(0);
            save(bagContent);
            bagContentList.add(bagContent);
            //背包更新
            userBag.setUsedAmount(userBag.getUsedAmount()+1);
        }

        //增加道具数量
        for(BagContent b:bagContentList){
            b.setAmount(b.getAmount()+amount);
            saveOrUpdate(b);
        }
    
        userBagService.saveOrUpdate(userBag);
    }

}
